package com.vstartek.dm.mongo.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.vstartek.dm.mongo.dao.VideoWebDao;
import com.vstartek.dm.mongo.model.Detail;
import com.vstartek.dm.mongo.model.VideoWeb;
@Repository
public class VideoWebDaoImpl extends BaseDao<VideoWeb> implements VideoWebDao {
	

	@Override
	public Class<VideoWeb> EntityClass() {
		return VideoWeb.class;
	}

	@Override
	public List<VideoWeb> findByList(VideoWeb t, int first, int max) {
		Query query = new Query();
		query.skip(first);
		query.limit(max);
		query.fields().exclude("details").exclude("description");
		if(t.getTitle()!=null)
		query.addCriteria(new Criteria().orOperator(Criteria.where("actor").regex(".*?" + t.getTitle() + ".*"),Criteria.where("title").regex(".*?" + t.getTitle() + ".*")));
		return this.getMongoTemplate().find(query, VideoWeb.class);
	}
	@Override
	public VideoWeb findById(Object id) {
		Query query = new Query(Criteria.where("id").is(id));
		query.fields().exclude("details");
		return	mongoTemplate.findOne(query, this.EntityClass());
	}

	@Override
	public List<Detail> fingdetailsByList(Object id) {
		Query query = new Query(Criteria.where("id").is(id));
		query.fields().include("details");
		return mongoTemplate.findOne(query, this.EntityClass()).getDetails();
	}

	@Override
	public Detail findDetailById(Object id, Object idx) {
		Query query = new Query(Criteria.where("id").is(id));
		Criteria criteria = Criteria.where("details.idx").is(idx);
		query.addCriteria(criteria);
		query.fields().include("details");
		return mongoTemplate.findOne(query, this.EntityClass()).getDetails().get(0);
	}

	@Override
	public Detail saveDetail(Object id, Detail detail) {
		Query query = new Query(Criteria.where("id").is(id));
		Update update = new Update();
		update.addToSet("details", detail);
		mongoTemplate.upsert(query, update, this.EntityClass());
		return detail;
	}

	@Override
	public boolean updateDetail(Object id, Detail detail) {
		
		Query query = new Query(Criteria.where("id").is(id));
		//Criteria criteria = Criteria.where("details.idx").is(idx);
		//query.addCriteria(criteria);
		
		return false;
	}

	@Override
	public List<String> group() {
		
		//mongoTemplate.aggregate<VideoWeb>(Aggregation.group("videoType"),this.EntityClass(),VideoWeb.class);
		//BasicDBObject groupOpt = new BasicDBObject();
		//mongoTemplate.aggregate(aggregation, inputType, outputType)
			//Aggregation a = new Aggregation();
		
       List<String>	list = new ArrayList<String>();	
		TypedAggregation<VideoWeb> type = new TypedAggregation<VideoWeb>(VideoWeb.class, Aggregation.group("videoType"));
		AggregationResults<VideoWeb> result = mongoTemplate.aggregate(type,"video_web",VideoWeb.class);
		Iterator<VideoWeb>	iterator = result.getMappedResults().iterator();
		while(iterator.hasNext()){
			
			list.add(iterator.next().getId());
		}
		return list;
	}
	
}
